#include "Touch.h"



uint8_t menu_num = 0;
bool Onoff_state = 0;
bool Onoff_num_state = 0;
bool menu_key_long_flag = 0;
bool feed_key_long_flag = 0;
bool Onoff_key_long_flag = 0;

bool menu_key_flag = 0;
bool feed_key_flag = 0;
bool Onoff_key_flag = 0;
bool feed_num_state = 0;

void touch_int(void)
{
  MR88FX02_Gpio_Set_Mode(TM_BIN0,INPUT);
	MR88FX02_Gpio_Set_Mode(TM_BIN1,INPUT);
	MR88FX02_Gpio_Set_Mode(TM_BIN2,INPUT);
	MR88FX02_Gpio_Set_Mode(TM_BIN3,INPUT);

	
}

int touch_get(void)
{
	if(BIN3_state==0)
	{
		if((BIN0_state==0)&&(BIN1_state==1)&&(BIN2_state==1))
		{
			return 2;
		}
		else if((BIN0_state==1)&&(BIN1_state==0)&&(BIN2_state==1))
		{
			return 3;
		}
		else if((BIN0_state==1)&&(BIN1_state==1)&&(BIN2_state==1))
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	return 0;
}


void Key_state_get(void)
{
	static unsigned long menu_trigger_time = 0;
	static unsigned long feed_trigger_time = 0;
	static unsigned long Onoff_trigger_time = 0;
	
	static bool menu_num_state = 0;

	static bool Onoff_num_state = 0;

	if(touch_get()==1)
	{
		menu_key_state = 2;   //检测到触摸灯变蓝
		if(millis() - menu_trigger_time>10)   //触摸时间超过20ms触摸生效
		{
			if(touch_get()==1)
			{
				menu_num_state = 1;
			}
		}
	}
	else 
	{
		menu_key_state  =1;
	}
	if(menu_num_state)
	{
		
		if(menu_key_state==1)
		{
			if(millis() - menu_trigger_time>1000)   //触摸时间超过2000ms触摸生效
			{
				menu_key_long_flag = 1;
				
			}
			else
			{
				menu_num++;
				if(menu_num>3)
				{
					menu_num = 0;
				}
			}
			menu_num_state = 0;
			
		}
	}
	
	
	if(touch_get()==2)
	{
		feed_key_state = 2;   //检测到触摸灯变蓝
		if(millis() - feed_trigger_time>10)   //触摸时间超过20ms触摸生效
		{
			if(touch_get()==2)
			{
				feed_num_state = 1;
			}
		}
	}
	else 
	{
		feed_key_state  =1;
	}
	if(feed_num_state)
	{
		feed_num_state = 0;
		if(feed_key_state==1)
		{
			if(millis() - feed_trigger_time>1000)   //触摸时间超过2000ms触摸生效
			{
				feed_key_long_flag = 1;
			}
			
		}
	}
	
	
	if(touch_get()==3)
	{
		Onoff_key_state = 2;   //检测到触摸灯变蓝
		if(millis() - Onoff_trigger_time>10)   //触摸时间超过20ms触摸生效
		{
			if(touch_get()==3)
			{
				Onoff_num_state = 1;
			}
		}
	}
	else 
	{
		Onoff_key_state  =1;
	}
	if(Onoff_num_state)
	{
		
		if(Onoff_key_state==1)
		{
			if(millis() - Onoff_trigger_time>1000)   //触摸时间超过2000ms触摸生效
			{
				Onoff_key_long_flag = 1;
			}
			else
			{
				Onoff_key_flag = 1;
			}
			Onoff_num_state = 0;
		}
	}
	if(touch_get()==0)
	{
		menu_trigger_time = millis();
		feed_trigger_time = millis();
		Onoff_trigger_time = millis();
	}
}


void menu_state_updata(void)
{
	Temperature_Set_OnOff=0;  //温度设置闪烁
	time_hour_OnOff=0;  //时设置闪烁
	time_minute_OnOff=0;  //分设置闪烁
	
	if(menu_num == 0)
	{
		temp_set_set = 1;
		time_hour_set = 1;
		time_minute_set = 1;
	}
	else if(menu_num == 1)
	{
		time_hour_set = 1;
		time_minute_set = 1;
		Temperature_Set_OnOff = 1;
		if(Encoder_Key)
		{
			menu_num = 0;
			Temperature_Set = Temperature_Set_display;
			first_temp_set = 1;
			Encoder_Key = 0;
		}
	}
	else if(menu_num == 2)
	{
		temp_set_set = 1;
		time_minute_set = 1;
		time_hour_OnOff = 1;
		if(Encoder_Key)
		{
			menu_num = 3;
			time_hour = time_hour_display;
			time_ms = time_hour_display*60*60*1000+time_minute_display*60*1000;
			Encoder_Key = 0;
		}
	}
	else if(menu_num == 3)
	{
		temp_set_set = 1;
		time_hour_set = 1;
		time_minute_OnOff = 1;
		if(Encoder_Key)
		{
			menu_num = 0;
			time_minute = time_minute_display;
			time_ms = time_hour_display*60*60*1000+time_minute_display*60*1000;
			Encoder_Key = 0;
		}
	}
	if(menu_num != 1)
	{
		Temperature_Set_display = Temperature_Set;
	}
	if(menu_num != 2)
	{
		time_hour_display = time_hour;
	}
	if(menu_num != 3)
	{
		time_minute_display = time_minute;
	}
	
}
